package com.ogawa.a7517.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.ogawa.a7517.DataManager;
import com.ogawa.a7517.activity.MainActivity7517;
import com.ogawa.a7517.bean.DeviceStateBean;
import com.ogawa.a7517.bean.MassageArmchair;
import com.trello.rxlifecycle3.components.support.RxFragment;
import com.umeng.analytics.MobclickAgent;

/**
 * Fragment 的基类封装
 * Created by HourGlassRemember on 2018/10/25.
 */
public abstract class BaseFragment extends RxFragment {

    protected View rootView;
    protected boolean isInit;
    private static final String TAG = "BaseFragment";
    protected MainActivity7517 activity7517;

    /*
     *FragmentTransaction 几个常用的方法:
     * 1、add：把一个 Fragment 添加到一个 Fragment 管理容器里，执行创建方法；
     * 2、remove：移除 Fragment 管理窗口中 Fragment 并销毁 onDestroyView、onDestroy；
     * 3、replace：跟 add 类似，同时移除 Fragment 管理容器中 id 相同的 Fragment；
     * 4、show：展示 add 到容器中的 Fragment 的 View（不会影响 Fragment 的 onCreate()、onCreateView、onViewCreate、onDestroy）；
     * 5、hide：隐藏 add 到容器中的 Fragment 的 View（不会影响 Fragment 的 onCreate()、onCreateView、onViewCreate、onDestroy）；
     * 6、detach：将 Fragment 的 View 从 Activity 中分离会执行 onDestroyView；
     * 7、attach：将 Fragment 的 View 附着到 Activity 中会执行 onCreateView、onViewCreate、onActivityCreate。
     */

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            savedInstanceState.clear();
        }
        activity7517 = DataManager.getInst().getActivity();
        if (rootView == null) {
            rootView = inflater.inflate(getLayoutId(), null);
            initView(rootView);
            isInit = true;
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }

    public abstract int getLayoutId();

    /**
     * 初始化视图
     *
     * @param view
     * @return
     */
    public abstract void initView(View view);

    /**
     * 创建 Fragment
     * 当一个 Fragment 被创建的时候，如果　FragmentTransaction.remove 后再次添加会再次调用此方法，
     * 而 hidden 再 show 或者 detach 再 attach 的时候不会再次实例化
     *
     * @param savedInstanceState
     */
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 在 onCreateView() 后触发的事件
     *
     * @param view
     * @param savedInstanceState
     */
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    /**
     * 初次创建的时候都会被执行，hide -> show 的时候这些方法(onCreateView + onCreateView + onActivityCreated)不会再次调用，
     * 而 detach -> attach 的时候会再次调用
     *
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    protected void setTitle(String title) {
        if (activity7517 != null) {
            activity7517.setTitle(title);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        boolean isMassageSkill = this instanceof MassageSkillFragment;
        boolean isGasSkill = this instanceof GasSkillFragment;
        boolean isSeniorAdjust = this instanceof SeniorAdjustFragment;
        boolean isAutoPage = this instanceof AutoPageFragment;
        boolean isRollHeat = this instanceof RollHeatFragment;
        if (activity7517 != null && !isMassageSkill && !isGasSkill && !isSeniorAdjust
                && !isAutoPage && !isRollHeat) {
            activity7517.setCurrentFragment(this);
        }
        MobclickAgent.onPageStart(getClass().getName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(getClass().getName());
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    /**
     * Fragment 的 View 被销毁
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    /**
     * Fragment 被移除时销毁
     */
    @Override
    public void onDestroy() {
        rootView = null;
        super.onDestroy();
    }

    void judgeToWhere(boolean senior) {
        DeviceStateBean stateBean = MassageArmchair.getInstance().getDeviceStatusBean();
        if (stateBean != null) {
            if (stateBean.getFunctions().getShoulderDetectCheck() != null) {
                int detect = Integer.parseInt(stateBean.getFunctions().getShoulderDetectCheck().getModelValue());
                switch (detect) {
                    case 3://检测中
                        activity7517.startToFragmentAddToStack(new BodyTypeFragment());
                        break;
                    case 4://检测完成
                        if (MassageArmchair.getInstance().isSeniorMassage() || senior) {
                            activity7517.startToFragmentAddToStack(new SeniorMassageFragment());
                        } else {
                            activity7517.startToFragmentAddToStack(new AutoMassageFragment());
                        }
                        break;
                    case 5://允许肩部微调
                        activity7517.startToFragmentAddToStack(new ShoulderCheckFragment());
                        break;
                    case 0:
                        DeviceStateBean.FunctionsBean.SeatStatusBean seatStatus = stateBean.getFunctions().getSeatStatus();
                        if (seatStatus != null) {
                            String auto = stateBean.getFunctions().getAutoProgram().getModelValue();
                            boolean isSeniorMassage = MassageArmchair.getInstance().isSeniorMassage();
                            if (Integer.parseInt(seatStatus.getModelValue()) == 0 && (Integer.parseInt(auto) > 0 || isSeniorMassage)) {
                                activity7517.startToFragmentAddToStack(new BodyTypeFragment());
                            } else {
                                if (isSeniorMassage || senior) {
                                    activity7517.startToFragmentAddToStack(new SeniorMassageFragment());
                                }
                            }
                        }
                        break;
                }
            }
        }
    }

    public void deviceStateChange() {

    }
}
